package com.ddd.framework.dto;

import com.ddd.framework.enums.ResponseCode;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;

/**
 * Response to caller
 *
 * @author guowm
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class Response extends DTO {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private String code;
    
    /**
     * 状态信息
     */
    private String message;


    public static Response success() {
        Response response = new Response();
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setMessage(ResponseCode.SUCCESS.getMessage());
        return response;
    }

    public static Response failure() {
        Response response = new Response();
        response.setCode(ResponseCode.FAIL.getCode());
        response.setMessage(ResponseCode.FAIL.getMessage());
        return response;
    }

    public static Response failure(String errCode, String errMessage) {
        Response response = new Response();
        response.setCode(errCode);
        response.setMessage(errMessage);
        return response;
    }

    public static Response of(int rows) {
        String code = rows > 0 ? ResponseCode.SUCCESS.getCode() : ResponseCode.FAIL.getCode();
        String message = rows > 0 ? ResponseCode.SUCCESS.getMessage() : ResponseCode.FAIL.getMessage();
        return failure(code, message);
    }

}
